<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./js/vue.js"></script>
</head>

<body>
    <div id="app">
        <hello-age1></hello-age1>
        <hello-age2></hello-age2>
    </div>
    <script>
        var hub = new Vue()
        Vue.component('hello-age1', {
            data: function() {
                return {
                    name: 'age1'
                }
            },
            template: `
            <div>
                <button @click="handle">像兄弟2出发</button>
            </div>
            `,
            methods: {
                handle: function() {
                    hub.$emit('name1', this.name)
                }
            },
            mounted: function() {
                hub.$on('name2', (val) => {
                    console.log(val)
                })
            }
        })
        Vue.component('hello-age2', {
            data: function() {
                return {
                    name: 'age2'
                }
            },
            template: `
            <div>
                <button @click="handle">像兄弟1出发</button>
            </div>
            `,
            methods: {
                handle: function() {
                    hub.$emit('name2', this.name)
                }
            },
            mounted: function() {
                hub.$on('name1', (val) => {
                    console.log(val)
                })
            }
        })
        var vm = new Vue({
            el: '#app',
            data: {
                age: 3
            }
        })
    </script>
</body>

</html>